/*******************************************************************************
 * Copyright 2010 Ray Ling
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.ring.rinject.test;

import org.junit.Before;
import org.junit.Test;
import org.ring.rinject.RinjectEnvironment;
import org.ring.rinject.Rinjector;
import org.ring.rinject.test.zoo.Animal;
import org.ring.rinject.test.zoo.Bone;
import org.ring.rinject.test.zoo.Cat;
import org.ring.rinject.test.zoo.Dog;
import org.ring.rinject.test.zoo.Fish;
import org.ring.rinject.test.zoo.Food;

import static org.junit.Assert.*;

public class ZooTest1 {

	Rinjector rinjector;

	@Before
	public void setUp() {
		rinjector = new Rinjector();

		try {
			rinjector.map(Cat.class).map(Dog.class).map(Fish.class).map(
					Bone.class);
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void test1() throws Exception {
		Animal cat = new Cat();
		Animal dog = new Dog();

		Food catFood1 = rinjector.env(cat).concrete(Food.class).newObject();
		Food catFood2 = rinjector.env(cat).concrete("food").newObject();
		Food catFood3 = rinjector.env(Cat.class).concrete(Food.class)
				.newObject();

		Food dogFood1 = rinjector.env(Dog.class).concrete("food").newObject();
		Food dogFood2 = rinjector.env("dog").concrete(Food.class).newObject();
		Food dogFood3 = rinjector.env("dog").concrete("food").newObject();

		assertTrue("Cat don't eat this: " + catFood1, cat.eat(catFood1));
		assertTrue("Cat don't eat this: " + catFood2, cat.eat(catFood2));
		assertTrue("Cat don't eat this: " + catFood3, cat.eat(catFood3));
		assertTrue("Dog don't eat this: " + dogFood1, dog.eat(dogFood1));
		assertTrue("Dog don't eat this: " + dogFood2, dog.eat(dogFood2));
		assertTrue("Cat don't eat this: " + dogFood3, dog.eat(dogFood3));

	}

	@Test
	public void test2() throws Exception {
		Animal cat = new Cat();
		Animal dog = new Dog();

		Food catFood1 = rinjector.newObject(cat, Food.class);
		Food catFood2 = rinjector.newObject(cat, "food");
		Food catFood3 = rinjector.newObject(Cat.class, Food.class);

		Food dogFood1 = rinjector.newObject(Dog.class, "food");
		Food dogFood2 = rinjector.newObject("dog", Food.class);
		Food dogFood3 = rinjector.newObject("dog", "food");

		assertTrue("Cat don't eat this: " + catFood1, cat.eat(catFood1));
		assertTrue("Cat don't eat this: " + catFood2, cat.eat(catFood2));
		assertTrue("Cat don't eat this: " + catFood3, cat.eat(catFood3));
		assertTrue("Dog don't eat this: " + dogFood1, dog.eat(dogFood1));
		assertTrue("Dog don't eat this: " + dogFood2, dog.eat(dogFood2));
		assertTrue("Cat don't eat this: " + dogFood3, dog.eat(dogFood3));

	}

	@Test
	public void test3() throws Exception {
		Animal cat = new Cat();
		Animal dog = new Dog();

		RinjectEnvironment env = rinjector.env(cat);

		Food catFood1 = env.concrete(Food.class).newObject();
		Food catFood2 = env.concrete("food").newObject();

		env = rinjector.env(dog);
		Food dogFood1 = env.concrete("food").newObject();
		Food dogFood2 = env.concrete(Food.class).newObject();

		assertTrue("Cat don't eat this: " + catFood1, cat.eat(catFood1));
		assertTrue("Cat don't eat this: " + catFood2, cat.eat(catFood2));
		assertTrue("Dog don't eat this: " + dogFood1, dog.eat(dogFood1));
		assertTrue("Dog don't eat this: " + dogFood2, dog.eat(dogFood2));

	}
}
